/*
 * Copyright 2011 Subhabrata Ghosh <subho.ghosh at langene.net>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */



package net.langene.nlp.model;

//~--- JDK imports ------------------------------------------------------------

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author Subhabrata Ghosh <subho.ghosh at langene.net>
 */
public class Entity {
    private HashMap<String, List<EnumConnectorType>>	connections;
    private Token					token;
    private EnumEntityType				type;

    /**
     * Create a new Entity associated with the specified Token
     * of the given Type.
     * 
     * @param token Token object
     * @param type Entity Type Enum
     */
    public Entity(Token token, EnumEntityType type) {
        connections = new HashMap<String, List<EnumConnectorType>>();
        this.type   = type;
        this.token  = token;
    }

    /**
     * Get the Token for the current Entity.
     *
     * @return Token
     */
    public Token getToken() {
        return this.token;
    }

    /**
     * Get the Entity Type.
     *
     * @return Entity Type Enum.
     */
    public EnumEntityType getType() {
        return this.type;
    }

    /**
     * Add a new Relationship to this Entity node.
     *
     * @param target Target token to define the relationship with.
     * @param type Connector Type.
     * @return true if added, false if already exists.
     */
    public synchronized boolean addRelationship(Token target, EnumConnectorType type) {
        if (!connections.containsKey(token.getWord())) {
            List<EnumConnectorType>	parts = new LinkedList<EnumConnectorType>();

            connections.put(token.getWord(), parts);
        }

        List<EnumConnectorType>	node = connections.get(token.getWord());

        for (EnumConnectorType re : node) {
            if (re == type) {
                return false;
            }
        }

        node.add(type);

        return true;
    }

    /**
     * Get the defined relationships between the current entity
     * and the target token.
     *
     * @param target Target token to fetch relationships for.
     * @return List of defined Connector Types or null if none exists.
     */
    public List<EnumConnectorType> getRelationships(Token target) {
        if (!connections.containsKey(token.getWord())) {
            return null;
        }

        return connections.get(token.getWord());
    }

    /**
     * Get the defined relationships between the current entity
     * and the target entity.
     *
     * @param target Target Entity to fetch relationships for.
     * @return List of defined Connector Types or null if none exists.
     */
    public List<EnumConnectorType> getRelationships(Entity target) {
        Token	tk = target.getToken();

        return getRelationships(tk);
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
